/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
    vector<vector<int> > Print(TreeNode* pRoot) {
        if(!pRoot) return {};
        TreeNode* tmp;
        vector<int> path;
        vector<vector<int>> ans;
        int size;
        deque<TreeNode*> q;
        bool flag = true;
        q.push_back(pRoot);
        while(!q.empty()) {
            size = q.size();
            if(flag) {
                for(int i = 0; i < size; i++){
                    tmp = q.front();
                    q.pop_front();
                    path.push_back(tmp->val);
                    if(tmp->left) q.push_back(tmp->left);
                    if(tmp->right) q.push_back(tmp->right);
                }
            }
            else {
                for(int i = 0; i < size; i++){
                    tmp = q.back();
                    q.pop_back();
                    path.push_back(tmp->val);
                    if(tmp->right) q.push_front(tmp->right);
                    if(tmp->left) q.push_front(tmp->left);
                }
            }
            flag = !flag;
            ans.push_back(path);
            path.clear();
        }
        return ans;
    }
    
};